<?php

namespace app\admin\controller\order;

use app\common\controller\Backend;
use app\admin\model\order\Category as CategoryModel;
use fast\Tree;

/**
 * 分类管理
 *
 * @icon fa fa-list
 */
class Category extends Backend {

    /**
     * @var \app\admin\model\order\Category
     */
    protected $model = null;
    protected $categorylist = [];
    protected $noNeedRight = ['selectpage','jsonList'];

    public function _initialize() {
        parent::_initialize();
        $this->request->filter(['strip_tags']);
        $this->model = model('app\admin\model\order\Category');

        $tree = Tree::instance();
        $tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'pid');
        $this->categorylist = $tree->getTreeList($tree->getTreeArray(0), 'title');
        $categorydata = [0 => ['title' => __('None')]];
        foreach ($this->categorylist as $k => $v) {
            $categorydata[$v['id']] = $v;
        }
//                dump($categorydata);

        $this->view->assign("parentList", $categorydata);
    }

    public function jsonList() {
        $where = ['pid'=>0];
        $datalist = $this->model->field('id,title')->where($where)->select();
        $jsonlist = array();
        foreach ($datalist as $item) {
            $jsonlist[$item['id']] = $item['title'];
        }
        return json($jsonlist);
    }

    /**
     * 查看
     */
    public function index() {
        if ($this->request->isAjax()) {
            //构造父类select列表选项数据
            $list = $this->categorylist;
            $total = count($list);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * Selectpage搜索
     * 
     * @internal
     */
    public function selectpage() {
        return parent::selectpage();
    }

}
